<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryRef" :inline="true" label-width="auto" v-show="showSearch">
            <el-form-item label="用户搜索">
                <el-input
                    v-model="queryParams.user_kwd"
                    placeholder="请输入用户ID，或昵称，或手机号"
                    clearable
                    @keyup.enter="handleQuery"
                    style="width: 300px"
                />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
                <el-button icon="Refresh" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                    type="primary"-->
<!--                    plain-->
<!--                    icon="Plus"-->
<!--                    @click="handleAdd"-->
<!--                    v-hasPermi="['system:user:add']"-->
<!--                >新增-->
<!--                </el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                    type="success"-->
<!--                    plain-->
<!--                    icon="Edit"-->
<!--                    :disabled="single"-->
<!--                    @click="handleUpdate"-->
<!--                    v-hasPermi="['system:user:edit']"-->
<!--                >修改-->
<!--                </el-button>-->
<!--            </el-col>-->
<!--            <el-col :span="1.5">-->
<!--                <el-button-->
<!--                    type="danger"-->
<!--                    plain-->
<!--                    icon="Delete"-->
<!--                    :disabled="multiple"-->
<!--                    @click="handleDelete"-->
<!--                    v-hasPermi="['system:user:remove']"-->
<!--                >删除-->
<!--                </el-button>-->
<!--            </el-col>-->
            <el-col :span="1.5">
                <el-button
                    type="warning"
                    plain
                    icon="el-icon-download"
                    @click="handleExport"
                    v-hasPermi="['system:user:export']"
                >导出
                </el-button>
            </el-col>
            <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-loading="loading" :data="list">
            <el-table-column type="selection" width="55" align="center"/>
            <el-table-column label="UID" align="center" prop="id"/>
            <el-table-column label="头像" align="center" prop="avatar">
                <template slot-scope="{row}">
                  <el-image
                    v-if="row.avatar"
                    style="width: 40px; height: 40px"
                    :src="row.avatar"
                    :preview-src-list="[row.avatar]">
                  </el-image>
                </template>
            </el-table-column>
            <el-table-column label="昵称|手机号" align="center" prop="nickName">
                <template slot-scope="{row}">
                    <div style="display: flex; flex-direction: column;">
                        <span>{{row.nickName}}</span>
                        <span>{{row.phone}}</span>
                    </div>
                </template>
            </el-table-column>
            <el-table-column label="注册时间" align="center" prop="createTime">
                <template slot-scope="{row}">
                    <span>{{ row.createTime }}</span>
                </template>
            </el-table-column>
            <el-table-column label="用户openid" align="center" prop="openid"/>
            <el-table-column label="钱包余额" align="center" prop="money"/>
            <el-table-column label="操作" width="180" align="center" class-name="small-padding fixed-width">
                <template slot-scope="{row}">
                    <el-button type="text" icon="el-icon-view" @click="handleDetail(row)"
                               v-hasPermi="['system:user:detail']">详情
                    </el-button>
<!--                    <el-button link type="primary" icon="Edit" @click="handleUpdate(row)"-->
<!--                               v-hasPermi="['system:user:edit']">修改-->
<!--                    </el-button>-->
<!--                    <el-button link type="primary" icon="Delete" @click="handleDelete(row)"-->
<!--                               v-hasPermi="['system:user:remove']">删除-->
<!--                    </el-button>-->
                </template>
            </el-table-column>
        </el-table>

        <pagination
          v-show="total>0"
          :total="total"
          :page.sync="queryParams.pageNum"
          :limit.sync="queryParams.pageSize"
          @pagination="getList"
        />

        <!-- 添加或修改对话框 -->
        <el-dialog :title="title" v-model="open" width="500px" teleported>
            <el-form ref="postRef" :model="form" :rules="rules" label-width="auto">
                <el-form-item label="合伙人名称">
                    <el-input v-model="form.name" placeholder="请输入合伙人名称"/>
                </el-form-item>
                <el-form-item label="手机号码">
                    <el-input v-model="form.phone" placeholder="请输入手机号码"/>
                </el-form-item>
                <el-form-item label="所属运营商" style="width: 100%;">
                    <el-select v-model="form.operat_id" placeholder="请选择">
                        <el-option
                            v-for="item in operatorOptions"
                            :key="item.id"
                            :label="item.OperatorName"
                            :value="parseInt(item.id)"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="密码">
                    <el-input v-model="form.password" placeholder="请输入密码"/>
                </el-form-item>
                <el-form-item label="用户ID">
                    <el-input v-model="form.todo" placeholder="请输入用户ID"/>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </template>
        </el-dialog>

        <el-dialog title="用户详情" :visible.sync="detailDialogVisible" width="70%" append-to-body>
            <Detail :uid="show_uid" />
        </el-dialog>
    </div>
</template>

<script>
import * as api from "@/api/user/user";
import {getAllOperator} from "@/api/station/billing_rules";
import Detail from "@/components/Common/user/Detail.vue";
export default {
  name: 'User',
  components: { Detail },
  props: {
    order_detail: {
      type: Object
    },
  },
  data() {
    return {
      form: {},// 表单参数
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      queryParams: {// 查询参数
        pageNum: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      rules: {},
      operatorOptions:[],//所属运营商
      list:[],//页面数据
      dateRange:[],
      detailDialogVisible:false,//用户详情弹窗
      show_uid:0,
    };
  },
  mounted(){
    this.initOperatorOptions()
    this.getList()
  },
  methods: {
    getList() {
      this.loading = true;
      let queryParams = {...this.queryParams}
      if(!this.dateRange) this.dateRange = []
      if(this.dateRange.length) {
        queryParams.startTime = this.dateRange[0]
        queryParams.endTime = this.dateRange[1]
      }
      api.listUser(queryParams).then(response => {
        this.list = response.rows || [];
        this.total = response.total || 0;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.queryParams = {// 查询参数
        pageNum: 1,
        pageSize: 10,
      }
      this.resetForm("queryRef");
      this.handleQuery();
    },
    handleDetail(row) {//用户详情
      this.show_uid = row.id
      this.detailDialogVisible = true
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(api.EXPORT_URL, {
          ...this.queryParams
      }, `recharge_${new Date().getTime()}.xlsx`);
    },
    initOperatorOptions() {
      getAllOperator().then(resp => {
        this.operatorOptions = resp.rows || []
      })
    },
  }
}
</script>
